<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class ktvModule extends StoreBaseModule
{
	public function index()
	{
		convert_req($_REQUEST);	
		$keyword = addslashes(htmlspecialchars(trim($_REQUEST['keyword'])));
		//获取当前页的团购商品列表
		require_once APP_ROOT_PATH.'app/Lib/page.php';
		$city_id = intval($GLOBALS['deal_city']['id']);
		//分页
		$page = intval($_REQUEST['p']);
		if($page==0)
		$page = 1;
		$limit = (($page-1)*app_conf("DEAL_PAGE_SIZE")).",".app_conf("DEAL_PAGE_SIZE");	
				
			
		$cid = intval($_REQUEST['cid']);
		$uname = addslashes(trim($_REQUEST['cid']));
		$cate_item = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."deal_cate where id = ".$cid." or (uname = '".$uname."' and uname <> '')");
					
		$condition = " deal_type=1 ";  //条件
			
		$base_param = $url_param = array(
				"ctl"   => "ktv",
				"cid"	=> addslashes(trim($_REQUEST['cid'])),
				"aid"	=>	intval($_REQUEST['aid']),
				"tid"	=>	intval($_REQUEST['tid']),
				"qid"	=>	intval($_REQUEST['qid']),
				"pid"	=>	intval($_REQUEST['pid']),
				"sid"	=>	intval($_REQUEST['sid']),
				"a"	=>	intval($_REQUEST['a']),
				"keyword"	=>	$keyword,
		);			
		//组装分组筛选
		if($_REQUEST['g']&&is_array($_REQUEST['g']))
		foreach ($_REQUEST['g'] as $k=>$v)
		{
			$url_param["g[".$k."]"] = addslashes(trim($v));
		}

		
		if(intval($_REQUEST['is_redirect'])==1)
		{
			app_redirect(url("store","index",$url_param));
		}
		$GLOBALS['tmpl']->assign("url_param",$url_param); //将变量输出到模板
			
		unset($url_param['keyword']);
		unset($url_param['minprice']);
		unset($url_param['maxprice']);
		$cache_param = $url_param;
		$cache_param['cid'] = $cate_item['id'];
		$cache_param['city_id'] = $city_id;
		$cache_param['deal_type'] = 1;
				
		$result = load_auto_cache("store_filter_nav_cache",$cache_param);
		
		$GLOBALS['tmpl']->assign("cate_all",$result['cate_all']);
		$GLOBALS['tmpl']->assign("area_all",$result['area_all']);
		
		$filter_url_param = $base_param;
		$filter_url_param['has_tuan'] = intval($_REQUEST['has_tuan']);
		$filter_url_param['has_daijin'] = intval($_REQUEST['has_daijin']);
		$filter_url_param['has_youhui'] = intval($_REQUEST['has_youhui']);
		$filter_url_param['has_event'] = intval($_REQUEST['has_event']);
		$filter_url_param['has_goods'] = intval($_REQUEST['has_goods']);
		$filter_url_param['is_verify'] = intval($_REQUEST['is_verify']);
		
		$GLOBALS['tmpl']->assign("filter_url_param",$filter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_tuan']==1)
		$tmpfilter_url_param['has_tuan'] = 0;
		else
		$tmpfilter_url_param['has_tuan'] = 1;
		$filter_url['has_tuan'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_daijin']==1)
		$tmpfilter_url_param['has_daijin'] = 0;
		else
		$tmpfilter_url_param['has_daijin'] = 1;
		$filter_url['has_daijin'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_youhui']==1)
		$tmpfilter_url_param['has_youhui'] = 0;
		else
		$tmpfilter_url_param['has_youhui'] = 1;
		$filter_url['has_youhui'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_event']==1)
		$tmpfilter_url_param['has_event'] = 0;
		else
		$tmpfilter_url_param['has_event'] = 1;
		$filter_url['has_event'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['has_goods']==1)
		$tmpfilter_url_param['has_goods'] = 0;
		else
		$tmpfilter_url_param['has_goods'] = 1;
		$filter_url['has_goods'] = url("store","index",$tmpfilter_url_param);
		
		$tmpfilter_url_param = $filter_url_param;
		if($filter_url_param['is_verify']==1)
		$tmpfilter_url_param['is_verify'] = 0;
		else
		$tmpfilter_url_param['is_verify'] = 1;
		$filter_url['is_verify'] = url("store","index",$tmpfilter_url_param);
		
		
		$GLOBALS['tmpl']->assign("filter_url",$filter_url);
		//输出大区	
		$seo_title = $GLOBALS['lang']['STORE_KTV_LIST'];
		$seo_keyword = $GLOBALS['lang']['STORE_KTV_LIST'];
		$seo_description = $GLOBALS['lang']['STORE_KTV_LIST'];
		$append_seo = "";
		$area_id = intval($_REQUEST['aid']);	
		$quan_id = intval($_REQUEST['qid']);
		if($area_id>0)
		{
			$area_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."area where id = ".$area_id);	
				$append_seo = $area_name;
			if($quan_id>0)
			{
							
				$kw_unicode = str_to_unicode_string($area_name);
				//有筛选
				$condition .=" and (match(locate_match) against('".$kw_unicode."' IN BOOLEAN MODE))";	
				
			}
			else
			{
				$ids = load_auto_cache("deal_quan_ids",array("quan_id"=>$area_id));
				$quan_list = $GLOBALS['db']->getAll("select `name` from ".DB_PREFIX."area where id in (".implode(",",$ids).")");
				$unicode_quans = array();
				foreach($quan_list as $k=>$v){
					$unicode_quans[] = str_to_unicode_string($v['name']);
				}
				$kw_unicode = implode(" ", $unicode_quans);
				$condition .= " and (match(locate_match) against('".$kw_unicode."' IN BOOLEAN MODE))";
			}	
		}
		
		$GLOBALS['tmpl']->assign("area_list",$result['area_list']);
		
		if($area_id>0)
		{
			//输出商圈
	
			if($quan_id>0)
			{
					$area_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."area where id = ".$quan_id);				
					$kw_unicode = str_to_unicode_string($area_name);
					//有筛选
					$condition .=" and (match(locate_match) against('".$kw_unicode."' IN BOOLEAN MODE)) ";	
					$append_seo = $append_seo.$area_name;			
			}
			
			$GLOBALS['tmpl']->assign("quan_list",$result['quan_list']);
		}
		
		
		 $avg_price = intval($_REQUEST['a']);
		 if($avg_price>0)
		 {
		   		$condition .=" and avg_price = ".$avg_price;
				$append_seo .= "人均".$GLOBALS['lang']['AVG_PRICE_'.$avg_price]."的";
		 }
		 $GLOBALS['tmpl']->assign("avg_price_all",$result['avg_price_all']);
		 $GLOBALS['tmpl']->assign("avg_price_list",$result['avg_price_list']);
		
		//输出分类
		$cate_id = $cate_item['id'];
		
		$GLOBALS['tmpl']->assign("cate_list",$result['cate_list']);	
		
		//输出小分类
		$deal_type_id = intval($_REQUEST['tid']);	
		$deal_cate_id = $cate_id;
		if($deal_cate_id>0)
		{			
			$GLOBALS['tmpl']->assign("scate_list",$result['scate_list']);			
			$GLOBALS['tmpl']->assign("tag_group",$result['tag_group']);
			
			$deal_cate_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."deal_cate where id = ".$deal_cate_id);			
			$deal_cate_name_unicode = str_to_unicode_string($deal_cate_name);
			$condition .= " and (match(deal_cate_match) against('".$deal_cate_name_unicode."' IN BOOLEAN MODE)) ";
			
			$cate_condition = " (match(deal_cate_match) against('".$deal_cate_name_unicode."' IN BOOLEAN MODE)) ";
			
			$append_seo.=$cate_item['name'];		
		}
			
		if($deal_type_id>0)
		{
			$deal_type_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."deal_cate_type where id = ".$deal_type_id);
			$deal_type_name_unicode = str_to_unicode_string($deal_type_name);
			$condition .= " and (match(deal_cate_match) against('".$deal_type_name_unicode."' IN BOOLEAN MODE)) ";
			$append_seo.=$deal_type_name;
		}
		
		//输出目的
		$purpose_id =  intval($_REQUEST['pid']);
		$purpose_type_id =  intval($_REQUEST['sid']);
		if($purpose_id > 0){
			$unicode_purpose = array();
			$purpose_name= $GLOBALS['db']->getOne("select name from ".DB_PREFIX."purpose_cate where id = ".$purpose_id);
			$append_seo = $append_seo.$purpose_name;
			$unicode_purpose[] = str_to_unicode_string($purpose_name);
			if($purpose_type_id > 0){
				$purpose_type_name = $GLOBALS['db']->getOne("select name from ".DB_PREFIX."purpose_cate_type where id = ".$purpose_type_id);
				$unicode_purpose[] = str_to_unicode_string($purpose_type_name);
				$append_seo = $append_seo.$purpose_type_name;
			}
			$kw_unicode = implode(" ", $unicode_purpose);
			$condition .= " and (match(purpose_match) against('".$kw_unicode."' IN BOOLEAN MODE))";
			
			$GLOBALS['tmpl']->assign("spurpose_list",$result['spurpose_list']);
		}
		$GLOBALS['tmpl']->assign("purpose_all",$result['purpose_all']);
		$GLOBALS['tmpl']->assign("purpose_list",$result['purpose_list']);
		
			
		$seo_title = $append_seo.$seo_title;
	    $seo_keyword = $append_seo.$seo_keyword;
	    $seo_description = $append_seo.$seo_keyword;	
		
	   $supplier_id = intval($_REQUEST['id']);
	  
	   if($supplier_id>0)
	   {
	   	   $supplier_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier where id = ".$supplier_id);
	   	   if($supplier_info)
	   	   {
	   	   	  $GLOBALS['tmpl']->assign("supplier_info",$supplier_info);
	   	   	  $condition.=" and supplier_id = ".$supplier_id;
	   	   	  
	   	   }
	   }
	   
	   $url_param['tag'] = addslashes($_REQUEST['tag']);
	   if($url_param['tag']){
	   		$kw_unicode = str_to_unicode_string($url_param['tag']);
			//有筛选
			$condition .=" and (match(tags_match) against('".$kw_unicode."' IN BOOLEAN MODE))";	
	   }
	   
	   if($_REQUEST['g']&&is_array($_REQUEST['g']))
	   {
	   		foreach($_REQUEST['g'] as $k=>$v)
	   		{
	   			if(trim($v)!="")
	   			{
		   			$kw_unicode = str_to_unicode_string($v);
		   			$condition .=" and (match(tags_match) against('".$kw_unicode."' IN BOOLEAN MODE))";	
	   			}
	   		}
	   }
	   
	   if($keyword){
	   		$GLOBALS['tmpl']->assign("keyword",$keyword);
	   		$kws_div = div_str($keyword);
			foreach($kws_div as $k=>$item)
			{
				$kw[$k] = str_to_unicode_string($item);
			}
			$kw_unicode = implode(" ",$kw);
			//有筛选
			$condition .=" and ((match(name_match,locate_match,deal_cate_match,tags_match) against('".$kw_unicode."' IN BOOLEAN MODE)) or name like '%".$keyword."%') ";	
	   }
	   
	
		if($filter_url_param['has_tuan']>0)
		$condition.=" and tuan_count > 0 ";
		
		if($filter_url_param['has_daijin']>0)
		$condition.=" and daijin_count > 0 ";
		
		if($filter_url_param['has_youhui']>0)
		$condition.=" and youhui_count > 0 ";
		
		if($filter_url_param['has_event']>0)
		$condition.=" and event_count > 0 ";
		
		if($filter_url_param['has_goods']>0)
		$condition.=" and shop_count > 0 ";
		
		if($filter_url_param['is_verify']>0)
		$condition.=" and is_verify = 1 ";
		
		
	   	$sort_field = es_cookie::get("store_sort_field");
		$sort_type = es_cookie::get("store_sort_type");
		
		if($sort_field!="default"&&$sort_field!="dp_count"&&$sort_field!="avg_point"&&$sort_field!="ref_avg_price")
		{
			$sort_field = "default";
		}
		if($sort_field=='default')
		{
			$sortby = " is_recommend desc,is_verify desc,dp_count desc ";
		}
		else
		{
			$sortby = $sort_field." ".$sort_type;
		}
		
		$GLOBALS['tmpl']->assign('sort_field',$sort_field);
		$GLOBALS['tmpl']->assign('sort_type',$sort_type);
			
	   $result = get_store_list($limit,0,$condition,$sortby,false,true);
	  
	   foreach($result['list'] as $k=>$v)
	   {
	   	if($v['tuan_count']>0)
	   	$result['list'][$k]['tuan'] = $GLOBALS['db']->getRow("select d.name,d.id from ".DB_PREFIX."deal as d left join ".DB_PREFIX."deal_location_link as l on l.deal_id = d.id where d.is_effect = 1 and d.is_delete = 0 and d.time_status <> 2 and l.location_id = ".$v['id']." limit 1");

	   	if($v['youhui_count']>0)
		$result['list'][$k]['youhui'] = $GLOBALS['db']->getRow("select y.name,y.id from ".DB_PREFIX."youhui as y left join ".DB_PREFIX."youhui_location_link as l on l.youhui_id = y.id where y.is_effect = 1 and (y.end_time = 0 or y.end_time > ".get_gmtime().") and l.location_id = ".$v['id']." limit 1");
	   	
	   }
	   
	   $GLOBALS['tmpl']->assign("list",$result['list']);
	   $page = new Page($result['count'],app_conf("DEAL_PAGE_SIZE"));   //初始化分页对象 		
	   $p  =  $page->show();
	   $GLOBALS['tmpl']->assign('pages',$p);
	   
		
		$site_nav[] = array('name'=>$GLOBALS['lang']['HOME_PAGE'],'url'=>url("index","index"));
		$site_nav[] = array('name'=>$GLOBALS['lang']['STORE_KTV_LIST'],'url'=>url("store","ktv"));
		$GLOBALS['tmpl']->assign("site_nav",$site_nav);
			
		
		//输出最新加入的商家
		$new_stores = get_store_list(5,0,$cate_condition," id desc ",false,true);
		$GLOBALS['tmpl']->assign("new_stores",$new_stores['list']);
		if(trim($cate_condition)!='')
		$rec_stores = get_store_list(5,0, $cate_condition." and is_recommend = 1 "," id desc ",false,true);
		else
		$rec_stores = get_store_list(5,0, " is_recommend = 1 "," id desc ",false,true);
		$GLOBALS['tmpl']->assign("rec_stores",$rec_stores['list']);
					
		$GLOBALS['tmpl']->assign("page_title",$seo_title);

		$GLOBALS['tmpl']->assign("page_keyword",$seo_keyword.",");
				
		$GLOBALS['tmpl']->assign("show_share",1);
		
		$GLOBALS['tmpl']->assign("page_description",$seo_description.",");
		$GLOBALS['tmpl']->display("store_ktv.html");		
	}
}
?>